package com.example.design.pattern.structure.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import com.example.design.pattern.structure.proxy.Subject;
import com.example.design.pattern.structure.proxy.impl.RealSubject;

public class RealSubjectInvocationHandler implements InvocationHandler {

    private Subject subject;
    
    public RealSubjectInvocationHandler(Subject subject) {
        this.subject = subject;
    }
    
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("before...");
        Object object = method.invoke(subject, args);
        System.out.println("after...");
        
        return object;
    }
    
    public static Subject getProxyInstance(Subject subject) {
        
        return (Subject) Proxy.newProxyInstance(subject.getClass().getClassLoader(), subject.getClass().getInterfaces(), new RealSubjectInvocationHandler(subject));
    }
    
    public static void main(String[] args) {
        Subject subject = new RealSubject();
        Subject proxySubject = getProxyInstance(subject);
        proxySubject.request();
    }
}
